﻿using System;
using System.Collections;
using System.Windows.Media;
using System.Data.OracleClient;
using TimeTableEasy.src.Database;
using System.Collections.Generic;

namespace TimeTableEasy.src.Core
{
    class TimeTable
    {
        private DateTime _DateBegin, _DateEnd;
        private Account _User;
        private bool _Side;
        private List<String> _NameCurses, _DateCurses, _SalleCurses;
        private List<byte> _HourCurses, _LenghtCurses;
        private List<Color> _ColorCurses;
        private List<bool> _IsDouble;

        public TimeTable(Account account)
        {
            _DateBegin = GetBeginOfWeek();
            _DateEnd = GetEndOfWeek();
            _User = account;

            _NameCurses = new List<string>();
            _DateCurses = new List<string>();
            _SalleCurses = new List<string>();
            _LenghtCurses = new List<byte>();
            _HourCurses = new List<byte>();
            _ColorCurses = new List<Color>();
            _IsDouble = new List<bool>();

            _Side = true;
        }

        public List<String> DateCurses
        {
            get { return _DateCurses; }
        }

        public List<String> NameCurses
        {
            get { return _NameCurses; }
        }

        public List<String> SalleCurses
        {
            get { return _SalleCurses; }
        }

        public List<byte> LenghtCurses
        {
            get { return _LenghtCurses; }
        }

        public List<byte> HourCurses
        {
            get { return _HourCurses; }
        }

        public List<Color> ColorCurses
        {
            get { return _ColorCurses; }
        }

        public List<bool> IsDouble
        {
            get { return _IsDouble; }
        }

        public DateTime DateEnd
        {
            get { return _DateEnd; }
        }

        public DateTime DateBegin
        {
            get { return _DateBegin; }
        }

        public void PreviousWeek()
        {
            this._DateBegin = this._DateBegin.AddDays(-7);
            this._DateEnd = this._DateEnd.AddDays(-7);
        }

        public void NextWeek()
        {
            this._DateBegin = this._DateBegin.AddDays(7);
            this._DateEnd = this._DateEnd.AddDays(7);
        }

        private static DateTime GetBeginOfWeek()
        {
            System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;
            return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - fdow));
        }

        private static DateTime GetEndOfWeek()
        {
            System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;
            return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - fdow) + 6);
        }

        public void GetPlaningClasse(String m_Campus, String m_Curses)
        {
            _NameCurses.Clear();
            _DateCurses.Clear();
            _HourCurses.Clear();
            _LenghtCurses.Clear();
            _ColorCurses.Clear();
            _SalleCurses.Clear();
            _IsDouble.Clear();
            _HourCurses.Clear();
            _LenghtCurses.Clear();

            OracleDataReader array = OracleConnector.Query("SELECT CAM_NUM FROM CAMPUS WHERE CAM_NOM='" + ((m_Campus == null) ? _User.Campus[0] : m_Campus) + "'");

            if (array == null) return;

            array.Read();

            uint temp = (uint)array.GetDecimal(0);

            array = OracleConnector.Query("SELECT DIS_COURSNUM, DIS_DATE, DIS_HEUREDEBUT, DIS_HEUREFIN, DIS_ENSEIGNANTNUM, SAL_NOM, SAL_ETAGE,  MOD_COULEUR FROM DISPENSER, SALLE, MODALITE WHERE DISPENSER.DIS_SALLENUM = SALLE.SAL_NUM AND DISPENSER.DIS_MODALITENUM = MODALITE.MOD_NUM AND DIS_CAMPUSNUM = "+array.GetDecimal(0)+" AND DIS_CURSUS = '"+((m_Curses==null)?_User.Cursus[0].ToString():m_Curses)+"' AND TO_DATE(DIS_DATE, 'dd/mm/rrrr') >= TO_DATE('"+this._DateBegin.ToString("dd/MM/yyyy")+"', 'dd/mm/rrrr') AND TO_DATE(DIS_DATE, 'dd/mm/rrrr') <= TO_DATE('"+this._DateEnd.ToString("dd/MM/yyyy")+"', 'dd/mm/rrrr') ORDER BY (DIS_DATE),(DIS_HEUREDEBUT)");

            if (array == null) return;

            while (array.Read())
            {
                String[] local = null;

                OracleDataReader m_Array = OracleConnector.Query("SELECT COU_NOM FROM COURS WHERE COU_NUM="+array.GetDecimal(0));
                
                if (m_Array == null) return;

                while (m_Array.Read())
                    _NameCurses.Add(m_Array.GetString(0));

                _DateCurses.Add(array.GetDateTime(1).DayOfWeek.ToString());

                _HourCurses.Add((byte)array.GetDecimal(2));

                _LenghtCurses.Add((byte)(array.GetDecimal(3) - array.GetDecimal(2)));

                _SalleCurses.Add(array.GetString(5) + " - Etage n°" + array.GetDecimal(6));

                local = array.GetString(7).Split(',');

                _ColorCurses.Add(Color.FromRgb(byte.Parse(local[0]), byte.Parse(local[1]), byte.Parse(local[2])));

                m_Array = OracleConnector.Query("SELECT 1 FROM DISPENSER, SALLE, MODALITE WHERE DISPENSER.DIS_SALLENUM = SALLE.SAL_NUM AND DISPENSER.DIS_MODALITENUM = MODALITE.MOD_NUM AND DIS_CAMPUSNUM = " + temp + " AND DIS_CURSUS = '" + ((m_Curses == null) ? _User.Cursus[0].ToString() : m_Curses) + "' AND Dis_Date = TO_DATE('" + array.GetDateTime(1).ToString("dd/MM/yyyy") + "', 'dd/mm/rrrr')  and Dis_CoursNum = " + array.GetDecimal(0) + " and Dis_HeureDebut = " + array.GetDecimal(2) + " and Dis_Date In (select Dis_Date from DISPENSER where Dis_Date = TO_DATE('" + array.GetDateTime(1).ToString("dd/MM/yyyy") + "', 'dd/mm/rrrr') and Dis_CoursNum <> " + array.GetDecimal(0) + "  AND DIS_CURSUS = '" + ((m_Curses == null) ? _User.Cursus[0].ToString() : m_Curses) + "' and Dis_HeureDebut = " + array.GetDecimal(2) + ")");

                if (m_Array == null) return;

                m_Array.Read();

                _IsDouble.Add(m_Array.HasRows);

                m_Array.Dispose();
            }

            array.Dispose();
        }

        public void GetPlaningFormateur(UInt32 m_Id)
        {
            _NameCurses.Clear();
            _DateCurses.Clear();
            _HourCurses.Clear();
            _LenghtCurses.Clear();
            _ColorCurses.Clear();
            _SalleCurses.Clear();
            _IsDouble.Clear();

            OracleDataReader array = OracleConnector.Query("select Cam_Nom, Sal_Etage, Sal_Nom, Mod_Couleur, Cou_Nom, Ide_ID, Ide_Nom, Ide_Prenom, Dis_Date, Dis_HeureDebut, Dis_HeureFin, Dis_Cursus from CAMPUS, SALLE, DISPENSER, MODALITE, COURS, ENSEIGNANT, IDENTITE where CAMPUS.Cam_Num = DISPENSER.Dis_CampusNum and SALLE.Sal_Num = DISPENSER.Dis_SalleNum and MODALITE.Mod_Num = DISPENSER.Dis_ModaliteNum and DISPENSER.Dis_EnseignantNum = ENSEIGNANT.Ens_Num and ENSEIGNANT.Ens_Num = IDENTITE.Ide_ID and COURS.Cou_Num = DISPENSER.Dis_CoursNum and Ide_ID = " + m_Id + " and Dis_Date >= TO_DATE('" + this._DateBegin.ToString("dd/MM/yyyy") + "', 'dd/mm/rrrr') and Dis_Date <= TO_DATE('" + this._DateEnd.Date.ToString("dd/MM/yyyy") + "', 'dd/mm/rrrr') order by (Dis_Date), (Dis_HeureDebut)");

            if (array == null) return;

            while (array.Read())
            {
                String[] local = null;

                _NameCurses.Add(array.GetString(4) + " " + array.GetString(0));

                _DateCurses.Add(array.GetDateTime(8).DayOfWeek.ToString());

                _HourCurses.Add((byte)array.GetDecimal(9));

                _LenghtCurses.Add((byte)(array.GetDecimal(10) - array.GetDecimal(9)));

                _SalleCurses.Add(array.GetString(2) + " - Etage n°" + array.GetDecimal(1));

                local = array.GetString(3).Split(',');

                _ColorCurses.Add(Color.FromRgb(byte.Parse(local[0]), byte.Parse(local[1]), byte.Parse(local[2])));

                _User.Campus.Add(array.GetString(0));

                _User.Cursus.Add(array.GetString(11));

                _IsDouble.Add(false);


            }

            array.Dispose();
        }

        public String Position(bool IsDouble)
        {
            String response = "";

            if (!IsDouble)
                response = "full";
            else
            {
                if (this._Side)
                    response = "right";
                else
                    response = "left";

                this._Side = !(this._Side);
            }

            return response;
        }
    }
}
